<?php
require_once 'sisco.php';

/**
 * @package sisco
 * @subpackage controller
 */
class Movimientos_protoweb extends Sisco
{

	public function __construct() 
	{
		parent::__construct();
	}

    protected function createPrepare(&$data)
    {
        
        
        
    
        // Collections of has one relationships
        $data['productos'] = new Producto();
        $data['productos']->get();
        $data['lotes'] = new Lote();
        $data['lotes']->get();
        $data['remitos'] = new Remito();
        $data['remitos']->get();
        $data['almacenes'] = new Almacen();
        $data['almacenes']->get();
        $data['usuarios'] = new Usuario();
        $data['usuarios']->get();
        $data['tipos_movimiento'] = new Tipo_Movimiento();
        $data['tipos_movimiento']->get();
    
        // Collections of has many to many relationships
    
    }

    protected function assignCreatePost(&$data)
    {
        if ( array_key_exists('cantidad', $this->input->post()) ) $data['movimiento']->cantidad = $this->input->post('cantidad');
        if ( array_key_exists('fecha', $this->input->post()) ) $data['movimiento']->fecha = $this->input->post('fecha');
        if ( array_key_exists('observacion', $this->input->post()) ) $data['movimiento']->observacion = $this->input->post('observacion');
    
        // has one relationships
        if ($this->input->post('producto')) {
            $data['producto'] = new Producto($this->input->post('producto'));
        }
        if ($this->input->post('lote')) {
            $data['lote'] = new Lote($this->input->post('lote'));
        }
        if ($this->input->post('remito')) {
            $data['remito'] = new Remito($this->input->post('remito'));
        }
        if ($this->input->post('almacen')) {
            $data['almacen'] = new Almacen($this->input->post('almacen'));
        }
        if ($this->input->post('usuario')) {
            $data['usuario'] = new Usuario($this->input->post('usuario'));
        }
        if ($this->input->post('tipo_movimiento')) {
            $data['tipo_movimiento'] = new Tipo_Movimiento($this->input->post('tipo_movimiento'));
        }
        
        // Many to many
    
        return true;
    }

    protected function afterCreateSuccess(&$data)
    {
        
        
        
        
    }

    protected function revertOnCreateError(&$data)
    {
        
        
        
        
    }

    protected function updatePrepare(&$data)
    {
        
        
        
    
        // Collections of has one relationships
        $data['productos'] = new Producto();
        $data['productos']->get();
        $data['lotes'] = new Lote();
        $data['lotes']->get();
        $data['remitos'] = new Remito();
        $data['remitos']->get();
        $data['almacenes'] = new Almacen();
        $data['almacenes']->get();
        $data['usuarios'] = new Usuario();
        $data['usuarios']->get();
        $data['tipos_movimiento'] = new Tipo_Movimiento();
        $data['tipos_movimiento']->get();
        
        // Collections of has many to many relationships
    
    }

    protected function assignUpdatePost(&$data)
    {
        if ( array_key_exists('cantidad', $this->input->post()) ) $data['movimiento']->cantidad = $this->input->post('cantidad');
        if ( array_key_exists('fecha', $this->input->post()) ) $data['movimiento']->fecha = $this->input->post('fecha');
        if ( array_key_exists('observacion',$this->input->post()) ) $data['movimiento']->observacion = $this->input->post('observacion');
    
        // has one relationships
        if ($this->input->post('producto')) {
            $data['producto'] = new Producto($this->input->post('producto'));
        }

        if ($this->input->post('lote')) {
            $data['lote'] = new Lote($this->input->post('lote'));
        }

        if ($this->input->post('remito')) {
            $data['remito'] = new Remito($this->input->post('remito'));
        }

        if ($this->input->post('almacen')) {
            $data['almacen'] = new Almacen($this->input->post('almacen'));
        }

        if ($this->input->post('usuario')) {
            $data['usuario'] = new Usuario($this->input->post('usuario'));
        }

        if ($this->input->post('tipo_movimiento')) {
            $data['tipo_movimiento'] = new Tipo_Movimiento($this->input->post('tipo_movimiento'));
        }

    
        // Many to many
        
    
        return true;
    }

    protected function afterUpdateSuccess(&$data)
    {
        
        
        
        
    }

    protected function revertOnUpdateError(&$data)
    {
        
        
        
        
    }

    protected function afterDeleteSuccess(&$data)
    {
        
        
        
        
    }

    protected function readPrepare(&$data)
    {
        
        
        
        
    }

    protected function saveEntity(&$data,$saveHasOne=true,$saveHasMany=true)
    {
        // saving attributes
        if ( ! $data['movimiento']->save()) {
            return false;
        }

        // saving one raleted
        if ($saveHasOne) {
            $relationships = array();
                if (array_key_exists('producto', $data)) {
                array_push($relationships, $data['producto']);
            }
                if (array_key_exists('lote', $data)) {
                array_push($relationships, $data['lote']);
            }
                if (array_key_exists('remito', $data)) {
                array_push($relationships, $data['remito']);
            }
                if (array_key_exists('almacen', $data)) {
                array_push($relationships, $data['almacen']);
            }
                if (array_key_exists('usuario', $data)) {
                array_push($relationships, $data['usuario']);
            }
                if (array_key_exists('tipo_movimiento', $data)) {
                array_push($relationships, $data['tipo_movimiento']);
            }
            
            if ( ! $data['movimiento']->save($relationships)) {
                return false;
            }
        }

        // saving many to many relationships
        if ($saveHasMany) {
                    }
        return true;
    }

        
        
        
    
} // class Movimientos

/* End of file movimientos_protoweb.php */
/* Location: ./application/controllers/movimientos_protoweb.php */